package com.yorick.common.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 马超凡
 * @Date: 2019/8/21 0021 16:04
 * @Description:
 */
public class ImgCreateUtil {
	public static void createImage1(String[] strArr, Font font,
								   int width, int image_height, int line_height,String time) throws Exception {

		FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
		int stringWidth = fm.charWidth('字');// 标点符号也算一个字
		System.out.println(stringWidth);
		int line_string_num = width % stringWidth == 0 ? (width / stringWidth) : (width / stringWidth) + 1;
		System.out.println("每行=" + line_string_num);

		List<String> listStr = new ArrayList<String>();
		List<String> newList = new ArrayList<String>();
		for (int h = 0; h < strArr.length; h++) {
			listStr.add(strArr[h]);
		}
		for (int j = 0; j < listStr.size(); j++) {
			if( listStr.get(j).length() > line_string_num){
				newList.add(listStr.get(j).substring(0,line_string_num));
				listStr.add(j+1,listStr.get(j).substring(line_string_num));
				listStr.set(j,listStr.get(j).substring(0,line_string_num));
			}else{
				newList.add(listStr.get(j));
			}
		}

		int a = newList.size();
		int b =  image_height / line_height; // 每张图片有多少行文字//每张文字有多少行文字
		//int imgNum = a % b == 0 ? (a / b) : (a / b) + 1;

		//for (int m = 0; m < imgNum; m++) {
		//String filePath = "C:\\test\\img\\" + time + ".png";
		String filePath = "/home/tomcat/h5_sichuan/01/apache-tomcat-9.0.6/webapps/IMG/" + time + ".png";
		File outFile = new File(filePath);
		// 创建图片
		BufferedImage image = new BufferedImage(width, image_height,
				BufferedImage.TYPE_INT_RGB);
			//透明背景  the begin
			Graphics2D g = image.createGraphics();
			image = g.getDeviceConfiguration().createCompatibleImage(width, image_height, Transparency.TRANSLUCENT);
			g.dispose();
			g=image.createGraphics();
			//透明背景  the end
			g.setClip(0, 0, width, image_height);
		//	g.setColor(Color.white); // 背景色白色
			g.fillRect(0, 0, width, image_height);
			g.setColor(Color.black);//  字体颜色黑色
			g.setFont(font);// 设置画笔字体


		// 获取Graphics2D

		Graphics2D g2d = image.createGraphics();

// ----------  增加下面的代码使得背景透明  -----------------

		image = g2d.getDeviceConfiguration().createCompatibleImage(width, image_height, Transparency.TRANSLUCENT);

		g2d.dispose();

		g2d = image.createGraphics();

		g2d.setColor(new Color(0,0,0));

		g2d.setStroke(new BasicStroke(1));

		g2d.setFont(font);

		// 每张多少行，当到最后一张时判断是否填充满
		int i1 = (image_height / line_height - newList.size()) / 2;
		if(newList.size()>1) {
			for (int i = 0; i < image_height / line_height; i++) {
				//int index = i + m * every_line;
				//if (newList.size() - 1 >= index) {
				//System.out.println("每行实际=" + newList.get(index).length());
				if(i>=newList.size()){
					break;
				}

				if((i+1)==newList.size()){
					//最后一行
					g2d.drawString(newList.get(i),(width-(40*newList.get(i).length()))/2 , line_height * (i + 1+i1) );
				}else {
					//不是最后一行的时候
					g2d.drawString(newList.get(i), 0, line_height * (i + 1+i1));
				}

				//}
			}
		}else {
			g2d.drawString(newList.get(0),(width-(40*newList.get(0).length()))/2 , line_height*(i1+1 ));
		}
		g2d.dispose();
		ImageIO.write(image, "png", outFile);// 输出png图片
	}
	public static void createImage2(String[] strArr, Font font,
									int width, int image_height, int line_height,String time) throws Exception {

		FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
		int stringWidth = fm.charWidth('字');// 标点符号也算一个字
		System.out.println(stringWidth);
		int line_string_num = width % stringWidth == 0 ? (width / stringWidth) : (width / stringWidth) + 1;
		System.out.println("每行=" + line_string_num);

		List<String> listStr = new ArrayList<String>();
		List<String> newList = new ArrayList<String>();
		for (int h = 0; h < strArr.length; h++) {
			listStr.add(strArr[h]);
		}
		for (int j = 0; j < listStr.size(); j++) {
			if( listStr.get(j).length() > line_string_num){
				newList.add(listStr.get(j).substring(0,line_string_num));
				listStr.add(j+1,listStr.get(j).substring(line_string_num));
				listStr.set(j,listStr.get(j).substring(0,line_string_num));
			}else{
				newList.add(listStr.get(j));
			}
		}

		int a = newList.size();
		int b =  image_height / line_height; // 每张图片有多少行文字//每张文字有多少行文字
		//int imgNum = a % b == 0 ? (a / b) : (a / b) + 1;

		//for (int m = 0; m < imgNum; m++) {
		//String filePath = "C:\\test\\img\\" + time + ".png";
		String filePath = "/home/tomcat/h5_sichuan/01/apache-tomcat-9.0.6/webapps/IMG/" + time + ".png";
		File outFile = new File(filePath);
		// 创建图片
		BufferedImage image = new BufferedImage(width, image_height,
				BufferedImage.TYPE_INT_RGB);
			//透明背景  the begin
			Graphics2D g = image.createGraphics();
			image = g.getDeviceConfiguration().createCompatibleImage(width, image_height, Transparency.TRANSLUCENT);
			g.dispose();
			g=image.createGraphics();
			//透明背景  the end
			g.setClip(0, 0, width, image_height);
		//	g.setColor(Color.white); // 背景色白色
			g.fillRect(0, 0, width, image_height);
			g.setColor(Color.black);//  字体颜色黑色
			g.setFont(font);// 设置画笔字体


		// 获取Graphics2D

		Graphics2D g2d = image.createGraphics();

// ----------  增加下面的代码使得背景透明  -----------------

		image = g2d.getDeviceConfiguration().createCompatibleImage(width, image_height, Transparency.TRANSLUCENT);

		g2d.dispose();

		g2d = image.createGraphics();

		g2d.setColor(new Color(255,255,255));

	//	g2d.setBackground(new Color(0,0,0));

		g2d.setStroke(new BasicStroke(1));

		g2d.setFont(font);

		// 每张多少行，当到最后一张时判断是否填充满
		int i1 = (image_height / line_height - newList.size()) / 2;
		if(newList.size()>1) {
			for (int i = 0; i < image_height / line_height; i++) {
				//int index = i + m * every_line;
				//if (newList.size() - 1 >= index) {
				//System.out.println("每行实际=" + newList.get(index).length());
				if(i>=newList.size()){
					break;
				}

				if((i+1)==newList.size()){
					//最后一行
					g2d.drawString(newList.get(i),(width-(40*newList.get(i).length()))/2 , line_height * (i + 1+i1) );
				}else {
					//不是最后一行的时候
					g2d.drawString(newList.get(i), 0, line_height * (i + 1+i1));
				}

				//}
			}
		}else {
			g2d.drawString(newList.get(0),(width-(40*newList.get(0).length()))/2 , line_height*(i1+1 ));
		}
		g2d.dispose();
		ImageIO.write(image, "png", outFile);// 输出png图片
	}
	public static void createImage3(String[] strArr, Font font,
									int width, int image_height, int line_height,String time) throws Exception {

		FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
		int stringWidth = fm.charWidth('字');// 标点符号也算一个字
		System.out.println(stringWidth);
		int line_string_num = width % stringWidth == 0 ? (width / stringWidth) : (width / stringWidth) + 1;
		System.out.println("每行=" + line_string_num);

		List<String> listStr = new ArrayList<String>();
		List<String> newList = new ArrayList<String>();
		for (int h = 0; h < strArr.length; h++) {
			listStr.add(strArr[h]);
		}
		for (int j = 0; j < listStr.size(); j++) {
			if( listStr.get(j).length() > line_string_num){
				newList.add(listStr.get(j).substring(0,line_string_num));
				listStr.add(j+1,listStr.get(j).substring(line_string_num));
				listStr.set(j,listStr.get(j).substring(0,line_string_num));
			}else{
				newList.add(listStr.get(j));
			}
		}

		int a = newList.size();
		int b =  image_height / line_height; // 每张图片有多少行文字//每张文字有多少行文字
		//int imgNum = a % b == 0 ? (a / b) : (a / b) + 1;

		//for (int m = 0; m < imgNum; m++) {
		//String filePath = "C:\\test\\img\\" + time + ".png";
		String filePath = "/home/tomcat/h5_sichuan/01/apache-tomcat-9.0.6/webapps/IMG/" + time + ".png";
		File outFile = new File(filePath);
		// 创建图片
		BufferedImage image = new BufferedImage(width, image_height,
				BufferedImage.TYPE_INT_RGB);
		/*	//透明背景  the begin
			Graphics2D g = image.createGraphics();
			image = g.getDeviceConfiguration().createCompatibleImage(width, image_height, Transparency.TRANSLUCENT);
			g.dispose();
			g=image.createGraphics();
			//透明背景  the end
			g.setClip(0, 0, width, image_height);
		//	g.setColor(Color.white); // 背景色白色
			g.fillRect(0, 0, width, image_height);
			g.setColor(Color.black);//  字体颜色黑色
			g.setFont(font);// 设置画笔字体*/


		// 获取Graphics2D

		Graphics2D g2d =(Graphics2D) image.getGraphics();
		g2d.setBackground(Color.WHITE);//设置背景色
		g2d.clearRect(0, 0, width, image_height);//通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
// ----------  增加下面的代码使得背景透明  -----------------

		/*image = g2d.getDeviceConfiguration().createCompatibleImage(width, image_height, Transparency.TRANSLUCENT);

		g2d.dispose();*/

		//g2d = image.createGraphics();

		g2d.setPaint(Color.black);//设置画笔,设置Paint属性
		g2d.setStroke(new BasicStroke(1));

		g2d.setFont(font);

		// 每张多少行，当到最后一张时判断是否填充满
		int i1 = (image_height / line_height - newList.size()) / 2;
		if(newList.size()>1) {
			for (int i = 0; i < image_height / line_height; i++) {
				//int index = i + m * every_line;
				//if (newList.size() - 1 >= index) {
				//System.out.println("每行实际=" + newList.get(index).length());
				if(i>=newList.size()){
					break;
				}

				if((i+1)==newList.size()){
					//最后一行
					g2d.drawString(newList.get(i),(width-(40*newList.get(i).length()))/2 , line_height * (i + 1+i1) );
				}else {
					//不是最后一行的时候
					g2d.drawString(newList.get(i), 0, line_height * (i + 1+i1));
				}

				//}
			}
		}else {
			g2d.drawString(newList.get(0),(width-(40*newList.get(0).length()))/2 , line_height*(i1+1 ));
		}
		g2d.dispose();
		ImageIO.write(image, "png", outFile);// 输出png图片
	}
}
